package cn.imnaer.service.rpc.scenic.process;

import cn.imnaer.database.MatchConst;
import cn.imnaer.service.persistence.model.State;
import cn.imnaer.service.persistence.model.consts.STATE;
import cn.imnaer.service.rest.view.model.Place;
import cn.imnaer.service.rpc.recommend.dao.StateDaoImpl;

import org.bson.BSONObject;
import org.bson.BasicBSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StateModuleProcess {

    public static List<Place> getStateList(String keywords) {
        List<Place> placeList = new ArrayList<Place>();
        if (keywords != null && !keywords.isEmpty()) {
            BSONObject mather = new BasicBSONObject();
            Pattern pattern = Pattern.compile("[a-zA-Z]*");
            Matcher matcher = pattern.matcher(keywords);
            BSONObject query = new BasicBSONObject();
            if (matcher.matches()) {
                mather.put(MatchConst._REGEX, "^"+keywords+".*");
                mather.put(MatchConst._OPTIONS,"m");
                query.put(STATE.pinyin,mather);
            } else {
            	if(keywords!=null && keywords.length()>1){
            	mather.put(MatchConst._REGEX, "^"+keywords+".*");
                query.put(STATE.stateName,mather);
            	}
            	
            }
            System.out.println(query);
            /**
             * 如果query为null或者query={}这种情况，那么会查询出所有的值
             */
         List<State> list = StateDaoImpl.getInstance().findAll(query,null);
         for (State state : list) {
        	  Place place = new Place();
              place.setAttribute(state);
              placeList.add(place);
		}
 
        }
        return placeList;

    }

}
